// app.ts
import { apis } from './apis/apis'
import { requestPromise } from './utils/requestPromise'

App({
  globalData: {
    detail: {},
  },
  async onLaunch() {
    await this.getDetail();
  },
  async getDetail() {
    let _th = this;
    let detailTime = wx.getStorageSync('detailTime') || null;
    let detailCache = wx.getStorageSync('detail');
    requestPromise({
      url: apis.getDetailTime,
      method: "GET",
      data: {},
    }).then((res1: any) => {
      if ((detailTime == null || detailTime < res1.data.data) || (detailCache == null || detailCache == '' || detailCache == undefined)) {
        requestPromise({
          url: apis.getDetail,
          method: "GET",
          data: {},
        }).then((res2: any) => {
          wx.removeStorageSync('detailTime');
          wx.removeStorageSync('detail');
          wx.setStorageSync('detailTime', res2.data.data.time);
          wx.setStorageSync('detail', res2.data.data.detail);
          _th.globalData.detail = res2.data.data.detail;
        })

      } else {
        _th.globalData.detail = detailCache;
      }
    })
  }
})